package com.huarui.user.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;
import org.ewaf.framework.util.HttpUtil;

import com.huarui.user.model.LoginHistory;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
import com.huarui.user.service.LoginHistoryService;

/**
 * xzz
 * 
 * @author Administrator
 * 
 */
public class LoginHistoryAction extends BaseAction {

	private static final long serialVersionUID = 1683750513909613904L;
	private LoginHistoryService loginHistoryservice;
	private String dir;
	private String sort;
	private int start;
	private int limit;
	private SessionMessage sessionMessage;
    private Map<String,Object> resultMap=new HashMap<String, Object>();
    

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setLoginHistoryservice(LoginHistoryService loginHistoryservice) {
		this.loginHistoryservice = loginHistoryservice;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public SessionMessage getSessionMessage() {
		return sessionMessage;
	}

	public void setSessionMessage(SessionMessage sessionMessage) {
		this.sessionMessage = sessionMessage;
	}

	public LoginHistoryService getLoginHistoryservice() {
		return loginHistoryservice;
	}

	public String index() {
		return "index";

	}

	public String queryOwner() throws IOException {
		// 准备jqGrid分页参数
		sessionMessage = UserHolder.getCurrentSessionMessage();
		ListModel<LoginHistory> lm = new ListModel<LoginHistory>();
		lm.setPageSize(limit);
		lm.setStart(start);
		lm.setOrderBy(sort);
		lm.setDesc("desc".equalsIgnoreCase(dir) ? true : false);

		Integer userId = sessionMessage.getUserId();

		ListModel<LoginHistory> listModel = null;
		try {
			listModel = loginHistoryservice.getLoginHistoryByCondition(lm,
					userId);
		} catch (Exception e) {

			resultMap.put("failure", "服务器正则升级中...");

			return "json";
		}

		JSONArray root = JSONArray.fromObject(listModel.getResult());

		// 分页信息
		resultMap.put("totalProperty", listModel.getTotal());
		resultMap.put("root", root);

		return "json";
	}
	
	public String queryOther() throws IOException {
		// 准备jqGrid分页参数
		sessionMessage = UserHolder.getCurrentSessionMessage();
		HttpServletRequest request=HttpUtil.getRequest();
		ListModel<LoginHistory> lm = new ListModel<LoginHistory>();
		lm.setPageSize(limit);
		lm.setStart(start);
		lm.setOrderBy(sort);
		lm.setDesc("desc".equalsIgnoreCase(dir) ? true : false);
		String user_id=request.getParameter("userId");
		Integer userId = 0;
		
		if (StringUtils.isNotBlank(user_id)) {

			userId = Integer.valueOf(user_id);

		}

		ListModel<LoginHistory> listModel = null;
		try {
			listModel = loginHistoryservice.getLoginHistoryByCondition(lm,
					userId);
		} catch (Exception e) {

			resultMap.put("failure", "服务器正则升级中...");

			return "json";
		}

		JSONArray root = JSONArray.fromObject(listModel.getResult());

		// 分页信息
		resultMap.put("totalProperty", listModel.getTotal());
		resultMap.put("root", root);

		return "json";
	}

}
